from base.base_page import BasePage
from selenium.webdriver.common.by import By
from time import sleep
import pytest


class LoginPage(BasePage):

    """登录页面元素"""
    phone_loc = (By.ID, 'log-account')
    pwd_loc = (By.ID, 'log-pwd')
    submit_loc = (By.ID, 'sub-btn')

    """登录页面动作"""
    def go_to_zhangwuyou(self, url):
        self.go_to_url(url)

    def login_zhangwuyou(self, phone='18170627636', password='7017441986414Tll'):
        """输入手机号"""
        self.send_keys(LoginPage.phone_loc, phone)
        self.send_keys(LoginPage.pwd_loc, password)
        self.click(LoginPage.submit_loc)
        sleep(5)


class HomePage(BasePage):

    """管家主页页面元素"""
    url_home = "https://main.kdzwy.com/guanjia/#/home"
    pop_up_window_loc = (By.XPATH, '//div[@class="liveImg"]')
    notice_loc = (By.XPATH, '//div[@id="app"]//div[@class="el-dialog" and @aria-label="dialog"]')
    close_notice = (By.XPATH, '//div[@id="app"]//div[@class="el-dialog" and @aria-label="dialog"]//button[@type="button"]/i')
    img_loc = (By.XPATH, '//div[@class="hd_logo gj_nav_fixed"]//img')
    close_pop_up_window_loc = (By.CSS_SELECTOR, '#app .self-mask-close')
    circle_select_loc = (By.XPATH, '//span[@class="el-input__suffix"]//i')
    circle_submit_loc = (By.XPATH, '//div[@class="el-scrollbar"]//span[text()="智慧财税一键无忧测试"]')
    tip_loc = (By.XPATH, '//div[@class="smartAsk-pop-content"]')      #问我吧提示
    close_tip_loc = (By.XPATH, '//div[@class="smartAsk-pop-content"]//span[text()="我知道了"]')
    menu_account_tax_loc = (By.XPATH, "//div[text()='记账报税']")
    menu_customer_management_loc = (By.XPATH, '//div[text()="客户管理"]')
    login_information_loc = (By.XPATH, "//li[@class='submenu_item']/span[text()='票税登录信息']")
    invoice_collection_loc = (By.XPATH, "//li[@class='submenu_item']/span[text()='发票采集']")
    customer_list_loc = (By.XPATH, '//li[@class="submenu_item"]/span[text()="客户列表"]')
    declare_taxes_loc = (By.XPATH, '//li[@class="menu_item gj_nav_fixed"]//span[text()="申报（非个税）"]')
    drop_down_loc = (By.XPATH, '//span[@class="iconfont iconxia hd_switch_arrow"]')
    close_all_tab_loc = (By.XPATH, '//li[@class="hd_switch_three"]')

    """管家主页页面动作"""
    """加载首页"""
    def goto_home(self):
        self.go_to_url(HomePage.url_home)

    """判断弹窗并关闭"""
    def pop_up_window_manage(self):
        try:
            window = self.wait_locate_element(HomePage.pop_up_window_loc)
            if window:
                self.click(HomePage.close_pop_up_window_loc)
        except:
            sleep(1)

    """判断公告并关闭"""
    def notice_manage(self):
        try:
            notice = self.wait_locate_element(HomePage.notice_loc)
            if notice:
                self.click(HomePage.close_notice)
        except:
            sleep(1)

    """选择圈子"""
    def select_circle(self):
        self.click(HomePage.circle_select_loc)
        sleep(1)
        self.click(HomePage.circle_submit_loc)
        sleep(2)

    """进入客户列表页面"""
    def enter_customer_list(self):
        self.move(HomePage.menu_customer_management_loc)
        sleep(2)
        self.click(HomePage.customer_list_loc)
        self.click(HomePage.img_loc)
        sleep(2)

    """进入票税登录信息页面"""
    def enter_login_information(self):
        self.move(HomePage.menu_account_tax_loc)
        sleep(1)
        self.click(HomePage.login_information_loc)
        self.click(HomePage.img_loc)
        sleep(3)

    """进入发票采集页面"""
    def enter_invoice_collection(self):
        self.move(HomePage.menu_account_tax_loc)
        sleep(1)
        self.click(HomePage.invoice_collection_loc)
        self.click(HomePage.img_loc)
        sleep(3)

    """进入申报（非个税）页面"""
    def enter_declare_taxes(self):
        self.move(HomePage.menu_account_tax_loc)
        sleep(1)
        self.click(HomePage.declare_taxes_loc)
        self.click(HomePage.img_loc)
        sleep(5)

    """点击空白(账无忧v6.0)"""
    def click_img(self):
        self.move_click(HomePage.img_loc)

    """返回登录结果"""
    def login_result(self):
        return self.get_value(HomePage.menu_account_tax_loc)

    """返回主页框架"""
    def default_content(self):
        self.switch_to_default_content()

    """关闭浏览器"""
    def quit(self):
        self.driver.quit()

    """关闭所有页签"""
    def close_all_tab(self):
        self.switch_to_default_content()
        self.click(HomePage.drop_down_loc)
        sleep(1)
        self.click(HomePage.close_all_tab_loc)
        sleep(2)


class CustomerList(BasePage):

    """客户列表页面元素"""
    tip_loc = (By.XPATH, '//div[@class="desc-img"]')
    close_tip_loc = (By.XPATH, '//div[@class="desc-img"]//span')
    add_customer_loc = (By.XPATH, '//span[@class="operateBtn"]//span[text()="新增客户"]')
    input_customer_name_loc = (By.XPATH, '//div[@class="el-dialog__body"]//input[@role="textbox"]')
    add_submit_loc = (By.XPATH, '//div[@aria-label="新增客户"]//span[text()="确 定"]')
    add_alert_loc = (By.XPATH, '//div[@role="alert"]//p[text()="新增客户成功"]')
    search_loc = (By.XPATH, '//span[@class="search"]//input[@placeholder="客户名称/编号/联系人/联系电话/业务员"]')
    first_checkbox = (By.XPATH, '//div[@class="content-wrap"]//tr[@class="el-table__row"]//span[@class="el-checkbox__inner"]')
    more_loc = (By.XPATH, '//span[@class="operateBtn"]//div[2]//i')
    delete_customer_loc = (By.XPATH, '//ul[@x-placement="bottom-end"]/li[text()="删除"]')
    delete_submit_loc = (By.XPATH, '//div[@aria-label="删除客户"]//span[text()="删除"]')
    delete_alert_loc = (By.XPATH, '//div[@role="alert"]//p[text()="删除客户成功"]')

    """客户列表页面动作"""
    """判断小提示存在并关闭"""
    def tip_manage(self):
        try:
            tip = self.wait_locate_element(CustomerList.tip_loc)
            if tip:
                self.click(CustomerList.close_tip_loc)
        except:
            sleep(1)

    """新增客户"""
    def add_customer(self, company):
        self.click(CustomerList.add_customer_loc)
        self.send_keys(CustomerList.input_customer_name_loc, company)
        sleep(0.5)
        self.click(CustomerList.add_submit_loc)
        sleep(2)

    """新增客户成功提示返回"""
    def add_customer_alert(self):
        return self.get_value(CustomerList.add_alert_loc)

    """删除客户"""
    def delete_customer(self, company):
        self.send_keys_enter(CustomerList.search_loc, company)
        sleep(3)
        self.move_click(CustomerList.first_checkbox)
        sleep(1)
        self.move(CustomerList.more_loc)
        sleep(1)
        self.click(CustomerList.delete_customer_loc)
        sleep(1)
        self.click(CustomerList.delete_submit_loc)
        sleep(1)

    """删除客户成功提示返回"""
    def delete_customer_alert(self):
        return self.get_value(CustomerList.delete_alert_loc)


class LoginInformation(BasePage):

    """票税登录信息页面元素"""
    iframe = (By.XPATH, '//div[@id="app"]/following-sibling::div[@class="taxInfoVerify-wraper"]/iframe')
    select_loc = (By.XPATH, '//input[@placeholder="搜索客户名称/客户编号/办税员手机号码/验证异常原因（采票）/验证异常原因（报税）/个税验证异常原因/采集异常原因"]')
    taxpayer_id_number_loc = (By.XPATH, '//div[@id="tax_number"]//input')
    account_phone = (By.XPATH, '//div[@id="tax-login-box"]//input[@filedname="account"]')
    password = (By.XPATH, '//div[@id="tax-login-box"]//input[@filedname="password"]')
    certify_loc = (By.XPATH, '//div[@id="zwy_advconsummarypanelap"]//span[text()="立即验证"]')
    recertify_loc = (By.XPATH, '//div[@id="zwy_advconsummarypanelap"]//span[text()="重新验证"]')
    company_name_loc = (By.XPATH, '//div[@class="kd-table"]//tbody//td[@data-code="zwy_mergelistcolumnap"]//div[@class="_1iwTooAh _3vK-UpRp link-cell-content link-color"]')
    certify_status_loc = (By.XPATH, '//div[@id="billlistap"]//tr[@data-rowindex="0"]//td[@data-code="invoice_state"]//span')
    close_loc = (By.XPATH, '//span[@class="_2gee2xdp"]/span[text()="关闭"]')
    refresh_loc = (By.XPATH, '//div[@id="list_refresh"]//span[text()="刷新"]')
    entetr_tax_bureau_toc = (By.XPATH, '//div[@id="billlistap"]//tr[@data-rowindex="0"]//td[@data-code="zwy_listoperationcolumnap"]//span[text()="登录税局"]')

    """税局"""
    tax_user_loc = (By.XPATH, '//a[@id="userName"]')

    """票税登录信息页面动作"""
    """进入票税登录信息iframe"""
    def enter_login_information_iframe(self):
        self.switch_to_frame(LoginInformation.iframe)

    """输入企业名称并搜索"""
    def input_select(self, company):
        self.send_keys_enter(LoginInformation.select_loc, company)
        sleep(2)

    # """登录税局"""
    # def enter_tax_bureau(self):
    #     self.click(LoginInformation.entetr_tax_bureau_toc)
    #     sleep(20)

    """点击企业名称进入登录信息页面"""
    def click_company_name(self):
        self.click(LoginInformation.company_name_loc)
        sleep(2)

    """输入纳税人识别号"""
    def input_taxpayer_id_number(self, taxpayer_id):
        self.send_keys_enter(LoginInformation.taxpayer_id_number_loc, taxpayer_id)
        sleep(1)

    """输入账号/手机号"""
    def input_account(self, account):
        self.send_keys(LoginInformation.account_phone, account)

    """"输入密码"""
    def input_password(self, pwd):
        self.send_keys(LoginInformation.password, pwd)
        sleep(1)

    """立即验证"""
    def click_certify(self):
        self.click(LoginInformation.certify_loc)
        sleep(3)

    """发起税局重新验证"""
    def recertify(self):
        self.click(LoginInformation.recertify_loc)
        sleep(3)

    """关闭当前页面"""
    def close(self):
        self.click(LoginInformation.close_loc)
        sleep(1)

    """"刷新"""
    def refresh(self):
        self.click(LoginInformation.refresh_loc)
        sleep(1)

    """税局验证状态"""
    def certify_status(self):
        return self.get_value(LoginInformation.certify_status_loc)

    """税局断言"""
    def tax_user(self):
        return self.get_value(LoginInformation.tax_user_loc)

class DeclareTaxes(BasePage):
    """申报（非个税）页面元素"""
    iframe = (By.XPATH, '//div[@id="tab-list"]//iframe')
    expansion_filtration_loc = (By.XPATH, '//span[text()="展开过滤"]')
    month_loc = (By.XPATH, '//div[@class="_22jg3w6v"]//span[@title="2024年01月 "]')
    drop_filter_loc = (By.XPATH, '//span[text()="收起过滤"]')
    search_loc = (By.XPATH, '//div[@class="_14BiGn4c"]/input')
    VAT_general_loc = (By.XPATH, '//div[@class="kd-table"]//tr[@data-rowindex="0"]//input')
    batch_fetch_loc = (By.XPATH, '//span[@class="_2gee2xdp"]/span[text()="批量取数"]')
    access_state_loc = (By.XPATH, '//div[@class="kd-table"]//tr[@data-rowindex="0"]//td[@data-code="entry_fetching"]')
    refresh_loc = (By.XPATH, '//span[@class="_2gee2xdp"]//span[text()="刷新"]')

    """申报（非个税）页面动作"""
    """进入申报（非个税）页面iframe"""
    def enter_declare_taxes_iframe(self):
        self.switch_to_frame(DeclareTaxes.iframe)

    """2024-01搜索客户名称"""



class InvoiceCollection(BasePage):

    """发票采集页面元素"""
    """一般纳税人"""
    iframe = (By.XPATH, '//iframe[@id="getInvoice1_CQ"]')
    search_loc = (By.XPATH, '//div[@class="_14BiGn4c"]/input')
    install_plug_windows_loc = (By.XPATH, '//div[@id="dialogShow"]')
    close_install_plug_loc = (By.XPATH, '//div[@id="dialogShow"]//i[@class="kdfont kdfont-guanbi8 kd-hover-color _1ltLb1eh"]')
    check_box_loc = (By.CSS_SELECTOR, '.kd-table-header-cell-content .kd-checkbox-input')
    invoice_collection_button_loc = (By.XPATH, '//span[@class="_2gee2xdp"]/span[text()="采集发票"]')
    collect_drop_down_loc = (By.XPATH, '//div[@id="collection"]/div[@class="LQ0lrxr9"]/span')
    income_output_collect_loc = (By.XPATH, '//ul[@class="_3dJhqFhJ"]/li/span[text()="进销项发票采集"]')
    submit_loc = (By.XPATH, '//div[@id="dialogShow"]//span[text()="立即提交"]')
    task_issue_loc = (By.XPATH, '//div[@id="kd-notification"]//div[@title="任务下发成功"]')
    refresh_loc = (By.XPATH, '//div[@id="zwy_refresh1"]//span[text()="刷新"]')
    output_status_loc = (By.XPATH, '//div[@id="gridview"]//tr[@data-rowindex="0"]//td[@data-code="outputtimestr"]//span')
    income_verified_status_loc = (By.XPATH, '//div[@id="gridview"]//tr[@data-rowindex="0"]//td[@data-code="inputtimestr"]//span')
    income_unverified_status_loc = (By.XPATH, '//div[@id="gridview"]//tr[@data-rowindex="0"]//td[@data-code="inputtimestr_unauth"]//span')
    enter_account_book_loc = (By.XPATH, '//div[@id="gridview"]//tr[@data-rowindex="0"]//td[@data-code="zwy_listoperationcolumnap"]//span[text()="进账簿"]')

    """小规模纳税人"""
    iframe_02 = (By.XPATH, '//iframe[@id="getInvoice2_CQ"]')
    tab_select_loc = (By.XPATH, '//li[@data-v-653106bc]/span[@title="小规模纳税人"]')

    """发票采集页面动作"""
    """一般纳税人"""
    """进入一般纳税人发票采集页面iframe"""
    def enter_invoice_collection_iframe(self):
        self.switch_to_frame(InvoiceCollection.iframe)

    """输入客户名称并搜素"""
    def input_company_search(self, company_name):
        self.send_keys_enter(InvoiceCollection.search_loc, company_name)
        sleep(2)

    """判断插件安装弹窗并关闭"""
    def install_plug_windows_manage(self):
        try:
            plug = self.wait_locate_element(InvoiceCollection.install_plug_windows_loc)
            if plug:
                print("exist")
                self.click(InvoiceCollection.close_install_plug_loc)
                sleep(1)
        except:
            sleep(1)

    """点击复选框选中采集发票的客户"""
    def opt_for_company(self):
        self.move_click(InvoiceCollection.check_box_loc)
        sleep(1)

    """进销项发票采集任务发起"""
    def income_output_invoice_collection(self):
        self.click(InvoiceCollection.collect_drop_down_loc)
        sleep(1)
        self.click(InvoiceCollection.income_output_collect_loc)
        sleep(2)
        self.click(InvoiceCollection.submit_loc)

    """采集发票任务下发成功提示"""
    def task_issue(self):
        return self.get_value(InvoiceCollection.task_issue_loc)

    """点击刷新"""
    def click_refresh(self):
        self.click(InvoiceCollection.refresh_loc)
        sleep(1)

    """销项发票采集状态"""
    def output_status_return(self):
        return self.get_value(InvoiceCollection.output_status_loc)

    """已认证进项发票采集状态"""
    def income_verified_status(self):
        return self.get_value(InvoiceCollection.income_verified_status_loc)

    """未认证进项发票采集状态"""
    def income_unverified_status(self):
        return self.get_value(InvoiceCollection.income_unverified_status_loc)

    """批量采集断言"""
    def batch_collect_invoice_result(self):
        account = 1
        while account < 30:
            account += 1
            sleep(30)
            self.click(InvoiceCollection.refresh_loc)
            output_status = self.get_value(InvoiceCollection.output_status_loc)
            income_verified_status = self.get_value(InvoiceCollection.income_verified_status_loc)
            income_unverified_status = self.get_value(InvoiceCollection.income_unverified_status_loc)
            if output_status == "采集中" \
                    or income_verified_status == "采集中" \
                    or income_unverified_status == "采集中":
                continue
            elif "2024" in output_status \
                    and "2024" in income_verified_status \
                    and "2024" in income_unverified_status:
                print('success')
                break
            else:
                print('fail')
                break
        pytest.assume("2024" in self.get_value(InvoiceCollection.output_status_loc))
        pytest.assume("2024" in self.get_value(InvoiceCollection.income_verified_status_loc))
        pytest.assume("2024" in self.get_value(InvoiceCollection.income_unverified_status_loc))

    """进账簿"""
    def enter_account_book(self):
        self.click(InvoiceCollection.enter_account_book_loc)
        sleep(5)

    """发票采集页面动作"""
    """小规模纳税人"""
    """进入小规模纳税人发票采集页面iframe"""
    def enter_invoice_collection_iframe_02(self):
        self.switch_to_frame(InvoiceCollection.iframe_02)

    """切换至小规模纳税人"""
    def tab_select(self):
        self.click(InvoiceCollection.tab_select_loc)
        sleep(1)


class Accountant(BasePage):

    """会计页面元素"""
    blank_loc = (By.XPATH, '//ul[@id="nav"]')
    intelligent_bookkeeping_loc = (By.XPATH, '//li[@class="item item-bill"]/a/span')
    income_invoice_loc = (By.XPATH, '//a[@id="incomeInvoice"]')
    iframe_income_loc = (By.ID, 'bill-incomeInvoice')
    invoice_tip_loc = (By.XPATH, '//div[@class="yunInvoice-box"]')
    invoice_know_loc = (By.ID, 'yunInvoiceKnow')
    intelligent_collect_loc = (By.XPATH, '//a[@id="more2"]/b')
    collect_invoice_loc = (By.XPATH, '//div[@id="getBillYQP"]')
    submit_loc = (By.XPATH, '//table[@class="ui_dialog"]//input[@value="确定"]')
    know_loc = (By.XPATH, '//table[@class="ui_dialog"]//input[@value="我知道了"]')
    refresh_loc = (By.XPATH, '//a[@id="refresh"]/b')
    output_invoice_loc = (By.XPATH, '//li[@class="item item-bill"]//ul[@class="sub-nav"]/li/a[text()="销项票"]')
    collecting_loc = (By.XPATH, '//div[@class="yunInvoice-wraper-note"]/span[4]')
    collected_loc = (By.XPATH, '//div[@class="yunInvoice-wraper-note"]/span[4]')

    """会计页面动作"""
    """进入进项票取票页面"""
    def enter_income_invoice(self):
        self.move(Accountant.intelligent_bookkeeping_loc)
        sleep(1)
        self.click(Accountant.income_invoice_loc)
        self.move_click(Accountant.blank_loc)
        sleep(10)

    """发起进项取票任务"""
    def initiate_income_invoice(self):
        self.switch_to_frame(Accountant.iframe_income_loc)
        try:
            tip = self.wait_locate_element(Accountant.invoice_tip_loc)
            if tip:
                self.click(Accountant.invoice_know_loc)
                sleep(1)
        except:
            sleep(1)
        self.move_click(Accountant.intelligent_collect_loc)
        sleep(1)
        self.click(Accountant.collect_invoice_loc)
        sleep(2)
        self.click(Accountant.submit_loc)
        sleep(5)
        self.click(Accountant.know_loc)
        sleep(1)

    """进项取票结果断言"""
    def income_invoice_result(self):
        account = 1
        while account < 30:
            account += 1
            sleep(30)
            self.click(Accountant.refresh_loc)
            status = self.get_value(Accountant.collecting_loc)
            if status == "取票中":
                continue
            elif status == "取票成功":
                print('success')
                break
            else:
                print('fail')
                break
        # assert "采集成功断言失败继续执行" == self.get_value(Accountant.collected_loc)
        pytest.assume("取票成功" == self.get_value(Accountant.collected_loc))
